Filter hook 'get_{$adjacent}_post_sort'

in WP Core File wp-includes/link-template.php at line 2004

View Source

get_{$adjacent}_post_sort

Filter Hook
Description
Filters the ORDER BY clause in the SQL for an adjacent post query. The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency, 'next' or 'previous'. Possible hook names include: - `get_next_post_sort` - `get_previous_post_sort`

Hook Information

File Location wp-includes/link-template.php View on GitHub
Hook Type Filter
Line Number 2004

Hook Parameters

Type Name Description
string $order_by The `ORDER BY` clause in the SQL.
WP_Post $post WP_Post object.
string $order Sort order. 'DESC' for previous post, 'ASC' for next.

Usage Examples

Basic Usage
<?php
// Hook into get_{$adjacent}_post_sort
add_filter('get_{$adjacent}_post_sort', 'my_custom_filter', 10, 3);

function my_custom_filter($order_by, $post, $order) {
    // Your custom filtering logic here
    return $order_by;
}

Source Code Context

wp-includes/link-template.php:2004 - How this hook is used in WordPress core
<?php
1999  	 *
2000  	 * @param string $order_by The `ORDER BY` clause in the SQL.
2001  	 * @param WP_Post $post    WP_Post object.
2002  	 * @param string  $order   Sort order. 'DESC' for previous post, 'ASC' for next.
2003  	 */
2004  	$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
2005  
2006  	$query        = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
2007  	$key          = md5( $query );
2008  	$last_changed = wp_cache_get_last_changed( 'posts' );
2009  	if ( $in_same_term || ! empty( $excluded_terms ) ) {

PHP Documentation

<?php
/**
	 * Filters the ORDER BY clause in the SQL for an adjacent post query.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `get_next_post_sort`
	 *  - `get_previous_post_sort`
	 *
	 * @since 2.5.0
	 * @since 4.4.0 Added the `$post` parameter.
	 * @since 4.9.0 Added the `$order` parameter.
	 *
	 * @param string $order_by The `ORDER BY` clause in the SQL.
	 * @param WP_Post $post    WP_Post object.
	 * @param string  $order   Sort order. 'DESC' for previous post, 'ASC' for next.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 3
  • File: wp-includes/link-template.php
Related Hooks

Related hooks will be displayed here in future updates.